
require('./json2.js')



var fs = require("fs")

var data = fs.readFileSync('appflood.json'); //阻塞的
var str = data.toString()
//eval() 函数可计算某个字符串，并执行其中的的 JavaScript 代码。 eval(x+17)
var obj = eval('(' + str + ')')


var mysql  = require('mysql');

var ads = obj.apps
var connection = mysql.createConnection({host: 'rdsea0aj9n7m3g48ta5u.mysql.rds.aliyuncs.com', database: 'ad688com', user:'ad688com',password : 'ad688com',port: '3306',dateStrings: true ,}); 

//var connection = mysql.createConnection({host: 'game1715.com', database: 'test', user:'t',password : 's123',port: '3306',dateStrings: true ,}); 
connection.connect() 


//将非stringl类型递归转化为string
function xToString(obj)
{
    if (obj==null)
        return null 

	if(typeof(obj) == "string")
		return obj
	else if (typeof(obj) == "boolean")
		if(obj === true)
			return 'true'
		else
			return 'false'
	else if (typeof(obj) == "number")
		return obj
	else if (typeof(obj) == "object")
	{
		if (obj instanceof Array == true)
		{
			var str = ''
			for (var i=0;i<obj.length;i++)
			{    
                if (i==0) 
                {
                    str = str + xToString(obj[i])
                }
                else
                {
                    str = str + ',' + xToString(obj[i])
                }
			}	
			//str = str + ']'
			return str
		}
		else
		{
			return JSON.stringify(obj) 	//json对象转换成json对符串 
		}
	}
}

var createSql = 'CREATE TABLE IF NOT EXISTS `api_appflood` ('+

'`defaultID` int(11) primary key NOT NULL auto_increment,'+
'`preview` text,'+
'`remaining_info` text,'+
'`url` text,'+
'`os` text,'+
'`price` text,'+
'`cap` text,'+
'`geo` text,'+
'`id` text,'+
'`name` text) '+
'ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;'

connection.query(createSql, function(err, rows, fields) { 
     if (err) {
             console.log('[query] - :'+err);
        return;
     }
     for (var i=0;i<ads.length;i++)
        {   
            console.log('INSERT params1:',i)  
            var ad = ads[i]
            var element =['defaultID','preview', 'remaining_info','url','os', 'price','cap','geo', 'id','name']
            var sql1 = '('
            for (var j=0;j<element.length;j++)
            {
                sql1 = sql1 + element[j] 
                if (j == element.length -1)
                    sql1 = sql1 + ')'
                else
                    sql1 = sql1 + ','
            }
            var  sql = 'INSERT INTO api_appflood' + sql1 + 'VALUES(?,?,?,?,?,?,?,?,?,?)';
            var  params = [];
            for (var j=0;j<element.length;j++)
            {   
                if (j==0)
                    params[j] = null
                else
                {
    
                    params[j] = xToString(ad.offer[0][element[j]]) || xToString(ad[element[j]]) 
                }
                      
            }
            connection.query(sql,params, 
                function(err, rows) 
                { 
                    if (err) 
                    {
                        console.log('INSERT error:',err.message);
                        return
                    }
                    console.log('INSERT success:',rows);    
                })

        }

});  









